﻿<%@ Control Language="VB" AutoEventWireup="false" CodeFile="Create.ascx.vb" Inherits="Control_StatusActionRule_Create" %>
<script type="text/javascript">
    function ajaxJquery() {
        $(function () {
            $(".TextBox:first").focus();
            $("#<% =ddlFunction.ClientID %>").change(function () {
                changeFunction();
                return true;
            });
            $("#<% =ddlStatus.ClientID %>").change(function () {
                $("#<% =txtStatus.ClientID %>").val($(this).val());
                return true;
            });
            $("#<% =ddlAction.ClientID %>").change(function () {
                $("#<% =txtAction.ClientID %>").val($(this).val());
                return true;
            });
            $("#<% =ddlNextStatus.ClientID %>").change(function () {
                $("#<% =txtNextStatus.ClientID %>").val($(this).val());
                return true;
            });

            $("#<% =btnSave.ClientID %>").click(function () {
                return CheckValid();
            });
        });
    };

    function CheckValid() {
        var error = "";
        var countbug = 0;
        //Khai báo các biến để validate
        var ddlFunction = $("#<% =ddlFunction.ClientID %>").val().trim();

        var ddlStatus = $("#<% =ddlStatus.ClientID %>").val().trim();
        var ddlAction = $("#<% =ddlAction.ClientID %>").val().trim();
        var ddlNextStatus = $("#<% = ddlNextStatus.ClientID%>").val().trim();

        if (ddlFunction == "0") {
            error += "<li>Vui lòng chọn Chức năng</li>";
            countbug += 1;
        }

        if (ddlStatus == "0") {
            error += "<li>Vui lòng chọn Trạng thái</li>";
            countbug += 1;
        }
        if (ddlAction == "0") {
            error += "<li>Vui lòng chọn Thao tác</li>";
            countbug += 1;
        }
        if (ddlNextStatus == "0") {
            error += "<li>Vui lòng chọn Trạng thái tiếp theo</li>";
            countbug += 1;
        }
        if (ddlNextStatus == ddlStatus) {
            error += "<li>Trạng thái tiếp theo phải khác trạng thái hiện tại</li>";
            countbug += 1;
        }
        if (countbug >= 1) {
            //remove all old error
            $('#Content_Create1_ValidationGroup li').remove();
            //show new list bug
            $("#Content_Create1_ValidationGroup").append(error);
            $('html, body').animate({ scrollTop: $(".Error").height() }, 800);
            return false;
        }
        $('#Content_Create1_ValidationGroup li').remove();

        return true;
    }
    function changeFunction() {
        var FunctionId = $("#<%= ddlFunction.ClientID %>").val();
        var ddlStatus = $("#<%= ddlStatus.ClientID %>");
        var ddlNextStatus = $("#<%= ddlNextStatus.ClientID %>")

        getAction(FunctionId);
        getStatus(FunctionId);
        return true;
    };

    function getStatus(FunctionId) {
        var ddlStatus = $("#<%= ddlStatus.ClientID %>");
        var ddlNextStatus = $("#<%= ddlNextStatus.ClientID %>");

        $.ajax({
            url: '<%=ResolveUrl("~/Services/wsAutoComplete.asmx/getStatusByStatus")%>',
            data: "{'FunctionId': '" + FunctionId + "'}",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                json = eval("(" + data.d + ")");
                $(function () {
                    ddlStatus.find('option').remove().end().append('<option value="0">--- Chọn --- </option>');
                    for (i = 0; i < json.length; i++) {
                        var _Text = json[i]["Title"];
                        var _Value = json[i]["StatusId"];
                        ddlStatus.append('<option value=' + _Value + '>' + _Text + '</option>');
                    };
                    ddlNextStatus.find('option').remove().end().append('<option value="0">--- Chọn --- </option>');
                    for (i = 0; i < json.length; i++) {
                        var _Text = json[i]["Title"];
                        var _Value = json[i]["StatusId"];
                        ddlNextStatus.append('<option value=' + _Value + '>' + _Text + '</option>');
                    };

                });

            }, //ham thuc thi neu thanh cong    
            error: function () {
                alert("Có lỗi xảy ra. Vui lòng kiểm tra đường truyền");
            } //ham thuc thi neu loi xay ra.
        });

    };

    function getAction(FunctionId) {
        var ddlAction = $("#<%= ddlAction.ClientID %>");
        $.ajax({
            url: '<%=ResolveUrl("~/Services/wsAutoComplete.asmx/getActionByFunction")%>',
            data: "{'FunctionId': '" + FunctionId + "'}",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                json = eval("(" + data.d + ")");
                $(function () {
                    ddlAction.find('option').remove().end().append('<option value="0">--- Chọn --- </option>');
                    for (i = 0; i < json.length; i++) {
                        var _Text = json[i]["Title"];
                        var _Value = json[i]["ActionId"];
                        ddlAction.append('<option value=' + _Value + '>' + _Text + '</option>');
                    }
                    // return true;
                });

            }, //ham thuc thi neu thanh cong    
            error: function () {
                alert("Có lỗi xảy ra. Vui lòng kiểm tra đường truyền");
            } //ham thuc thi neu loi xay ra.
        });
    };
   
</script>
<asp:UpdatePanel ID="uplSta" runat="server">
    <ContentTemplate>
        <div class="content_porletform">
            <div class="HeadTitle">
                <h3>
                    <asp:Label ID="Label3" runat="server" Text="THÊM QUY TRÌNH" /></h3>
            </div>
            <div class="DivRequired">
                <span class="fieldRequired">&nbsp;</span>Trường yêu cầu nhập dữ liệu
            </div>
            <div class="Error">
                <div id="Content_Create1_ValidationGroup">
                    <ul>
                    </ul>
                </div>
            </div>
            <div class="DivRow">
                <div class="DivLabel">
                    <asp:Label ID="lblFunctionid" CssClass="TextLabel" runat="server" Text="Chức năng:" /><span
                        class="fieldRequired">&nbsp;</span>
                </div>
                <div class="DivTextBox">
                    <asp:DropDownList ID="ddlFunction" runat="server" CssClass="TextBox">
                    </asp:DropDownList>
                </div>
            </div>
            <div class="DivRow">
                <div class="DivLabel">
                    <asp:Label ID="Label1" CssClass="TextLabel" runat="server" Text="Trạng thái:" /><span
                        class="fieldRequired">&nbsp;</span>
                </div>
                <div class="DivTextBox">
                    <asp:DropDownList ID="ddlStatus" runat="server" CssClass="TextBox">
                    </asp:DropDownList>
                </div>
            </div>
            <div class="DivRow">
                <div class="DivLabel">
                    <asp:Label ID="lblActionid" CssClass="TextLabel" runat="server" Text="Thao tác:" /><span
                        class="fieldRequired">&nbsp;</span>
                </div>
                <div class="DivTextBox">
                    <asp:DropDownList ID="ddlAction" runat="server" CssClass="TextBox">
                    </asp:DropDownList>
                </div>
            </div>
            <div class="DivRow">
                <div class="DivLabel">
                    <asp:Label ID="lblNextstatusid" CssClass="TextLabel" runat="server" Text="Trạng thái tiếp theo:" /><span
                        class="fieldRequired">&nbsp;</span>
                </div>
                <div class="DivTextBox">
                    <asp:DropDownList ID="ddlNextStatus" runat="server" CssClass="TextBox">
                    </asp:DropDownList>
                </div>
            </div>
            <div class="DivRow">
                <div class="DivLabel">
                    &nbsp;&nbsp;&nbsp;</div>
                <div class="DivTextBox">
                    <div style="float: left">
                        <asp:Button ID="btnSave" CausesValidation="false" runat="server" Text=" Lưu " CssClass="btn" />
                        &nbsp;<asp:Button ID="btnReset" runat="server" CausesValidation="false" CssClass="btn"
                            Text="Hủy" /></div>
                    <div style="float: right; text-align: right">
                    </div>
                </div>
            </div>
            <div style="clear: both; height: 5px">
            </div>
            <div style="display: none">
                <asp:HiddenField ID="hidID" Value="0" runat="server" />
                <asp:TextBox ID="txtStatus" runat="server"></asp:TextBox>
                <asp:TextBox ID="txtAction" runat="server"></asp:TextBox>
                <asp:TextBox ID="txtNextStatus" runat="server"></asp:TextBox>
            </div>
        </div>
    </ContentTemplate>
</asp:UpdatePanel>
